﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.Web.UI;
using System.Text;

namespace Runda.CMS.Portal.SalesManage
{
    public partial class CancelAssociate : BasePage
    {
        public string Budgetcustomercode
        {
            get
            {
                if (Request.QueryString["Budgetcustomercode"] != null) return Request.QueryString["Budgetcustomercode"].ToString();
                else return "";
            }

        }
        public string Budgetcategorycode
        {
            get
            {
                if (Request.QueryString["Budgetcategorycode"] != null) return Request.QueryString["Budgetcategorycode"].ToString();
                else return "";
            }

        }
        public string sales
        {
            get
            {
                if (Request.QueryString["sales"] != null) return Server.UrlDecode(Request.QueryString["Sales"].ToString());
                else return "";
            }

        }
        public string BudgetUnit
        {
            get
            {
                if (Request.QueryString["BudgetUnit"] != null) return Server.UrlDecode(Request.QueryString["BudgetUnit"].ToString());
                else return "";
            }

        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(1);
            }
           
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid(1);
        }

        protected void btnRefershBillInfo_Click(object sender, EventArgs e)
        {
            
        }

        
        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "cancel")
            {
                string sql = @"insert into BudgetUnitRef_hist
                            (
                            RefGUID
                            ,BillUnit
                            ,CustomerCode
                            ,categoryCode
                            ,BudgetUnit
                            ,BudgetCustomerCode
                            ,BudgetCategoryCode
                            ,Sales
                            ,OperatorName
                            ,recorddate
                            ,CancelDate
                            )
                            select
                            RefGUID
                            ,BillUnit
                            ,CustomerCode
                            ,categoryCode
                            ,BudgetUnit
                            ,BudgetCustomerCode
                            ,BudgetCategoryCode
                            ,Sales
                            ,OperatorName
                            ,recorddate
                            ,GETDATE()
                            from BudgetUnitRef where RefGUID='{0}';delete BudgetUnitRef where  RefGUID='{0}'";

                string refGUID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RefGUID"].ToString();

                string sqlStr = string.Format(sql, refGUID);

                BillBll bll = new BillBll();

                bll.Executesql(sqlStr);
                BindGrid(1);

            }
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnCancel = e.Item.FindControl("lnkbtnCancel") as LinkButton;
                if (lnkbtnCancel != null)
                {
                    DataRowView dv = e.Item.DataItem as DataRowView;
                    if (dv["OperatorName"].ToString()!=base.CurrentUserEntity.UserName)
                    { lnkbtnCancel.Visible = false; }

                    //lnkEdit.NavigateUrl = "~/Budget/SalesbudgetDetail.aspx?AssistID=" + dv["AssistID"].ToString();
                    lnkbtnCancel.OnClientClick = "return OnDelConfirm();";
                }
            }
        }

        private void BindGrid(int pageindex)
        {
            string strsql = " and Budgetcustomercode = '" + Budgetcustomercode + "' and Budgetcategorycode='" + Budgetcategorycode + "' and sales='" + sales + "' and BudgetUnit='" + BudgetUnit + "'";
            DataTable dt = new BillBll().AssociatedSaleUnit(strsql);

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        

        protected void gridAccessoryList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            BindGrid(1);
       
        }

        

        
    }
}
